package com.vogue.sms.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Base64;
import java.util.Date;
import java.util.Map;

import com.vogue.sms.common.exception.RRException;
import com.vogue.sms.common.validator.ValidatorUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.vogue.sms.entity.ApiRecordDataEntity;
import com.vogue.sms.service.ApiRecordDataService;
import com.vogue.sms.common.utils.PageUtils;
import com.vogue.sms.common.utils.R;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;


/**
 * 数据记录
 *
 * @author George
 * @email qiaozhi_china@163.com
 * @date 2018-12-03 01:54:50
 */
@RestController
@RequestMapping("/api/record")
public class ApiRecordDataController {
    @Autowired
    private ApiRecordDataService ApirecordDataService;

    @PostMapping("content")
    @ApiOperation("记录数据")
    public R content(@RequestParam String deviceid,@RequestParam String imei,@RequestParam String type, @RequestParam String sendNumber, @RequestParam String receiveNumber, @RequestParam String content, @RequestParam Long time){
        System.out.println("设备ID："+deviceid+" IMEI："+imei+" 类型："+type+" 发送号码："+sendNumber+" 接收号码："+receiveNumber+" 时间戳："+time);
        System.out.println("内容："+content);

        if ("call".equals(type)){
            if (content.length()<=0){
                content="文件长度为零";
            }else {
               File file= new File("/tmp/" + sendNumber+""+receiveNumber+""+time+".3gp");
                if (base64ToFile(content,file.getAbsolutePath())){
                    content=file.getAbsolutePath();
                }else {
                    content="太长数据啦";
                }
            }
        }

        ApiRecordDataEntity dataEntity=new ApiRecordDataEntity();
        dataEntity.setContent(content);
        dataEntity.setImei(imei);
        dataEntity.setReceiveNumber(receiveNumber);
        dataEntity.setSendNumber(sendNumber);
        dataEntity.setType(type);
        dataEntity.setRecordTime(new Date(time));
        ApirecordDataService.insert(dataEntity);
        return R.ok("你成功啦").put("data","其他数据");
    }

    /**
     * 上传文件
     */
    @PostMapping("upload")
    public R upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        return R.ok("你成功啦");
    }

    public boolean base64ToFile(String base64,String path){
        // 对字节数组字符串进行Base64解码并生成图片
        System.out.println("base64："+base64);
        System.out.println("path："+path);
        if (base64 == null)
            return false;
        try {
            // Base64解码
            byte[] b = new BASE64Decoder().decodeBuffer(base64);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    // 调整异常数据
                    b[i] += 256;
                }
            }
            // 生成Jpeg图片
            OutputStream out = new FileOutputStream(path);
            out.write(b);
            out.flush();
            out.close();
            System.out.println("out.close()");
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }
}